package com.andro.inje.chansung.department;

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.andro.inje.chansung.ConstantName;
import com.andro.inje.chansung.ConstantURL;
import com.andro.inje.chansung.DB;

import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Handler;

public class DepartmentIntroUpdateThread extends Thread {
	
	private Activity activity;
	private Handler handler;
	
	public DepartmentIntroUpdateThread(Activity activity, Handler handler) {
		this.activity = activity;
		this.handler  = handler;
	}
	
	public void run() {
		try {
			Document doc = Jsoup.connect(ConstantURL.DEPARTMENT_INTRO_URL).get();
			Department department = new Department(doc.select("name").get(0).text(),
													doc.select("phone").get(0).text(),
													doc.select("fax").get(0).text(),
													doc.select("location").get(0).text(),
													doc.select("introduction").get(0).text(),
													doc.select("feature").get(0).text(),
													doc.select("learning_subject").get(0).text(),
													doc.select("job_realted").get(0).text());
			SQLiteDatabase db = activity.openOrCreateDatabase(ConstantName.DATABASE_NAME, Context.MODE_WORLD_WRITEABLE, null);
			DB.insertDataIntoDepartmentTable(db, department);
			db.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		handler.sendEmptyMessage(0);
	}
}
